<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../taglibs.jsp"%>
<%@ include file="../easyuiLib.jsp"%>
<%@page import="com.carnation.utils.SessionInfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute("sessionInfo"); %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
	$(function() {
		tabClose();
		tabCloseEven();
		$('#caidan').tree({
			onSelect : function(node) {
				var url = "${ctx}" + node.attributes.url;
				var title = node.text;
				var icon = node.iconCls;
				addTab(title, url, icon);
			}
		});
		$.ajax({
			url : '${ctx}/TreeMenu',
			cache : false,
			success : function(data) {
				$('#caidan').tree("loadData", JSON.parse("[" + data + "]"));
			}
		});
	});

	function refresh() {
		$.ajax({
			url : '${ctx}/TreeMenu',
			cache : false,
			success : function(data) {
				$('#caidan').tree("loadData", JSON.parse("[" + data + "]"));
			}
		});
	}

	function createFrame(url) {
		console.log(url);
		return '<iframe scrolling="auto" frameborder="0"  src="' + url
				+ '" style="width:100%;height:100%;"></iframe>';
	}

	function tabClose() {
		$(".tabs-inner").dblclick(function() {
			var subtitle = $(this).children(".tabs-closable").text();
			$('#s').tabs('close', subtitle);
		});

		$(".tabs-inner").bind('contextmenu', function(e) {
			$('#mm').menu('show', {
				left : e.pageX,
				top : e.pageY
			});
			var title = $(this).children(".tabs-closable").text();
			$('#s').tabs('select', title);
			return false;
		});
	}

	function tabCloseEven() {
		$('#mm-tabupdate').click(function() {
			console.log('update');
			var currTab = $('#s').tabs('getSelected');
			if(currTab.panel('options').title != "welcome"){
				var url = $(currTab.panel('options').content).attr('src');
				$('#s').tabs('update', {
					tab : currTab,
					options : {
						content : createFrame(url)
					}
				});
			}
		});

		$('#mm-tabclose').click(function() {
			console.log('close');
			var title = $('#s').tabs('getSelected').panel('options').title;
			if (title != "welcome") {
				$('#s').tabs('close', title);
			}
		});

		$('#mm-tabcloseall').click(function() {
			console.log('closeall');
			var panels = $('#s').tabs('tabs');
			for (var i = panels.length - 1; i >= 0; i--) {
				var title = panels[i].panel('options').title;
				if (title != "welcome") {
					$('#s').tabs('close', title);
				}
			}
		});

		$('#mm-tabcloseother').click(function() {
			console.log('other');
			var currtitle = $('#s').tabs('getSelected').panel('options').title;
			var panels = $('#s').tabs('tabs');
			for (var i = panels.length - 1; i >= 0; i--) {
				var t = panels[i].panel('options').title;
				if (t != "welcome" && currtitle != t) {
					$('#s').tabs('close', t);
				}
			}
		});
	}

	function addTab(title, url, icon) {
		if (!$('#s').tabs('exists', title)) {
			$('#s').tabs('add', {
				title : title,
				content : createFrame(url),
				closable : true,
				iconCls : icon
			});
		} else {
			$('#s').tabs('select', title);
			$('#mm-tabupdate').click();
		}
		tabClose();
	}
	function logout(){
		$.ajax({
			url:'${ctx}/Logout',
			cache:false,
			success:function(data){
				var json = JSON.parse(data);
				console.log(data);
				if(json.status == "200"){
					location.replace("${ctx}/login.jsp");
				}
				else{
					$.messager.alert("标题","发送未知错误,请联系系统管理员");
				}
			}
		});
	}
</script>
<title>菜单管理</title>
</head>
<body class="easyui-layout" fit="true" split="true" border="false">
	<div region="north" split="true" border="false"
		style="overflow: hidden; height: 70px; background-color: #F1F1F1; line-height: 20px; color: #fff; font-family: Verdana, 微软雅黑, 黑体">
		<span style="float: right; padding-right: 20px; color: blue;"
			class="head"><span>欢迎使用试用平台 用户:<span style="text-decoration:underline;"><%=sessionInfo.getLoginName() %></span></span><br /><span style="float:right;"><a href="javascript:void(0);" onclick="logout()">安全退出</a></span></span> <span
			style="float: left; padding-left: 0px; font-size: 16px;"> <img
			src="images/logo.png" width="529" height="66" />
		</span>
	</div>
	<div region="south" split="true"
		style="height: 30px; background: #D2E0F2;">
		<div
			style="text-align: center; font-size: 15px; color: blue; font-weight: bold;">
			<span>By xiemb Email:<a href="mailto:xiemb@carnation.com.cn">xiemb@carnation.com.cn</a></span>
		</div>
	</div>
	<div region="west" id="west" width="250px;" hide="true" split="true"
		title="菜单管理" data-options="tools:'#tt'">
		<ul id="caidan" class="easyui-tree" animate="true">

		</ul>
	</div>
	<div region="center" id="center" >
		<div id="s" class="easyui-tabs" fit="true" border="false">
			<div title="welcome">
				<h1>欢迎来到目录树管理页面</h1>
			</div>
		</div>
	</div>
	<div id="mm" class="easyui-menu" style="width: 150px;">
		<div id="mm-tabupdate">刷新</div>
		<div id="mm-tabclose">关闭</div>
		<div id="mm-tabcloseall">全部关闭</div>
		<div id="mm-tabcloseother">除此之外全部关闭</div>
	</div>
	<div id="tt">
		<a class="icon-reload easyui-tooltip" title="刷新目录树" onclick="refresh()"></a>
	</div>
</body>
</html>